package teehee.UI;
import teehee.Form.SubmitInfoForm;
import teehee.Manager.BmiManager;
import teehee.homepage.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

/**
 * @author Naseem Safa
 *
 */
public class Bmi extends Activity
{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bmi);
        
     
    }
    public void bmi(View view) {
	    Intent intent_one = new Intent(Bmi.this, HealthHistory.class);
	    EditText curWeight = (EditText) findViewById(R.id.editText3);
	    String weight = curWeight.getText().toString().trim();
	    intent_one.putExtra("weightnum", weight);
	    EditText curHeight = (EditText) findViewById(R.id.editText4);
	    String height = curHeight.getText().toString().trim();
	    intent_one.putExtra("heightnum", height);
	    
	   

	    	boolean isWeight = SubmitInfoForm.valid(weight);
	    	boolean isHeight = SubmitInfoForm.valid(height);
	         
       
        if(isWeight && isHeight)
        	{
        			
        		double BMI = BmiManager.calculateBMI(weight, height);
        		Context context = getApplicationContext();
        		CharSequence text = "Your BMI is " + BMI;
       			int duration = Toast.LENGTH_LONG;

       			Toast toast = Toast.makeText(context, text, duration);
       			toast.setGravity(Gravity.TOP|Gravity.CENTER, 25, 300);
        		toast.show();
        		
       		}
        else
        {
        	Context context = getApplicationContext();
    		CharSequence text = "Height or Weight not correct. 0/empty/non number is not allowed";
   			int duration = Toast.LENGTH_LONG;

   			Toast toast = Toast.makeText(context, text, duration);
   			toast.setGravity(Gravity.TOP|Gravity.CENTER, 25, 300);
    		toast.show();
        }
        curWeight.setText(null);
        curHeight.setText(null);
        
	}
    
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if ((keyCode == KeyEvent.KEYCODE_SEARCH)) //replace "MENU" with "SEARCH" to use the search button
        { 
        	//setContentView(R.layout.search); //xml file to display, found in res/layout
        	Intent homep = new Intent(Bmi.this, SearchPage.class); //XXX.this is the current page. YYY.class is the destination page.
    		startActivity(homep);
            return true;
        }
        
        if ((keyCode == KeyEvent.KEYCODE_MENU)) //replace "MENU" with "SEARCH" to use the search button
        { 
        	//setContentView(R.layout.search); //xml file to display, found in res/layout
        	Intent homep = new Intent(Bmi.this, MainPage.class); //XXX.this is the current page. YYY.class is the destination page.
    		startActivity(homep);
            return true;
        }
        return super.onKeyDown(keyCode, event);    
    } 
}
    